<?php
/**
 * Created by PhpStorm.
 * User: jiankian
 * Date: 2017/4/8
 * Time: 上午9:51
 */
//前台访问控制器
namespace app\expbao\home;
use app\expbao\common\controller\ExUcenter;
use app\expbao\model\ExUser as ExUserModel;
use app\expbao\model\ExOpt as ExOptModel;
use app\expbao\model\ExOptUser as ExOptUserModel;
use think\Hook;
class Ucenter extends ExUcenter
{
//前台访问控制器
    public function index()
    {
        $action=$_SERVER['PATH_INFO'];
        //$action_rule=
        //echo EID ."</br>";
       // echo $_SERVER['PATH_INFO']."</br>";
        $user_id=$this->is_exp_login();
        if($user_id){
        	//echo "已经登录";
           $test="已经登录";
         
                           //$opt=ExOptModel::where('id','1')->select();
                //var_dump(ExOptModel::getlastsql());
                //var_dump($opt);
           //$userinfo = ExUserModel::where($user_id)->find();
           $userinfo=ExUserModel::getuserinfo($user_id);
           $useropt=ExUserModel::getuseropt($user_id);
           $opt=ExOptModel::select();
           //$opt='';
            //$useropts=explode(',', $opt['oid'], -1);
//             $actived = ExOptUserModel::table('__EXPBAO_OPERATION_USER__')
//             ->alias('t1')
// //            ->field('t1.id,t1.status,t1.gc_name,t1.pid,t2.gc_name as pname')
//             ->join('expbao_operation t2','t1.oid = t2.id','LEFT')
//             ->where('uid',$user_id)
//             ->order('order','asc')
//             ->select();
            //var_dump(ExOptUserModel::getlastsql());
            $opt_array=ExOptModel::select();
            $user_opt_value=ExOptUserModel::where('uid',$user_id)->value('oid');
            $user_opt_array=explode(",",$user_opt_value);
            if ($opt_array) {
                    $acyes = [];
                    $acno=[];
                  foreach ($opt_array as $k => $v) {
                    if (in_array($v['id'], $user_opt_array))
                          {
                          //echo "</br>"."匹配已找到".$v['opt'];
                          
                          array_push($acyes,$v);
                          //var_dump($acyes);
                          }
                        else
                          {
                          //echo "</br>"."匹配未找到".$v['opt'];
                          
                          array_push($acno,$v);
                          //var_dump($acno);
                          }
                    }
            }

            //var_dump(ExOptModel::getlastsql());
//             $unactive = ExOptUserModel::table('__EXPBAO_OPERATION__')
//             ->alias('t1')
// //            ->field('t1.id,t1.status,t1.gc_name,t1.pid,t2.gc_name as pname')
//             ->join('expbao_operation_user t2','t1.id = t2.oid','LEFT')
//             //->where('isactive',null)
//             ->order('order','asc')
//             ->select();
//            //var_dump($data_list);
//            var_dump(ExOptUserModel::getlastsql());
//            $map['uid'] = array('neq',$user_id);
//            $data_list = ExOptUserModel::table('__EXPBAO_OPERATION_USER__')
//             ->alias('t1')
// //            ->field('t1.id,t1.status,t1.gc_name,t1.pid,t2.gc_name as pname')
//             ->join('expbao_operation t2','t1.oid = t2.id','LEFT')
//             ->where($map)
//             ->order('order','asc')
//             ->select();
             //var_dump(ExOptUserModel::getlastsql());
        //$this->assign('useropt', $useropts);
        //var_dump($unactive);
        $this->assign('user_id', $user_id);
        $this->assign('userinfo', $userinfo);
        $this->assign('test', $test);
        $this->assign('opt', $opt);
        $this->assign('acyes', $acyes);
        $this->assign('acno', $acno);
        //$this->assign('actived', $actived);
        //$this->assign('unactive', $unactive);
        }else{
        	$test="未登录";
            //echo "没有登录";
        	//$xxx=ExUserModel::test();
        	//echo $xxx;
           //$this->error("没有登录");
           $this->error('没有登录', url('ucenter/login'));
        }
        
        return $this->fetch(); // 渲染模板
    }
   
    public function login()
    {
        if ($this->request->isPost()) {
            // 获取post数据
            $data = $this->request->post();
            $rememberme = isset($data['remember-me']) ? true : false;

            // 登录钩子
            $hook_result = Hook::listen('signin', $data);
            if (!empty($hook_result) && true !== $hook_result[0]) {
                $this->error($hook_result[0]);
            }
            
            if (config('captcha_signin')) {
                $captcha = $this->request->post('captcha', '');
                $captcha == '' && $this->error('请输入验证码');
                if(!captcha_check($captcha, '', config('captcha'))){
                    //验证失败
                    $this->error('验证码错误或失效');
                };
            }
            $UserModel = new ExUserModel;
            $uid = $UserModel->login($data['member_username'], $data['member_passwd'], $rememberme);
            if ($uid) {
                // 记录行为
                //action_log('user_signin', 'admin_user', $uid, $uid);
                $this->success('登录成功', url('ucenter/index'));
            } else {
                $this->error($UserModel->getError());
            }
                
          }
          else{
          		return $this->fetch(); 

          	 }


    }

    /**
     * 退出登录
     * @author 蔡伟明 <314013107@qq.com>
     */
    public function logout()
    {
        session(null);
        cookie('eid', null);
        //cookie('signin_token', null);

        return $this->redirect('login');
    }


    
    public function reg($value='')
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            
            $result = $this->validate($data, 'ExUser');
            // 验证失败 输出错误信息
            if(true !== $result) return $this->error($result);

            if ($user = ExUserModel::create($data)) {
                // 记录行为
                //action_log('member_add', 'member', $user['id'], UID);
                  $opts = ExOptModel::column('id');
                  $opt_reg=implode(",", $opts);
                  $useropt = ExOptUserModel::create([
                            'uid'  =>  $user['id'],
                            'oid' =>  $opt_reg,
                            'isactive' => '1'
                        ]);
                    // foreach ($opts as &$opt) {
                    //     var_dump($opt);

                    //     if($opt<=2){
                    //         $useropt = ExOptUserModel::create([
                    //         'uid'  =>  $user['id'],
                    //         'oid' =>  $opt,
                    //         'isactive' => '1'
                    //     ]);
                    //     }else{
                    //         $useropt = ExOptUserModel::create([
                    //         'uid'  =>  $user['id'],
                    //         'oid' =>  $opt,
                    //         'isactive' => '0'
                    //     ]);
                    //     }
                    // }

                        //var_dump($opts);
                
                return $this->success('用户:'.$user['member_name'].'  '.'新增成功', url('login'));
            } else {
                return $this->error('新增失败');
            }
        } else{
            return $this->fetch();
        }
    }

    public function profile()
    {
       if ($this->request->isPost()) {

        } else {
            $user_id=$this->is_exp_login();
            if($user_id){
            //echo "已经登录";
           //$test="已经登录";
           //$userinfo = ExUserModel::where($user_id)->find();
           $userinfo=ExUserModel::getuserinfo($user_id);
           $useropt=ExUserModel::getuseropt($user_id);
            $useropts=explode(',', $useropt['oid'], -1);
           //var_dump($useropts);
           $data_list = ExOptUserModel::table('__EXPBAO_OPERATION_USER__')
            ->alias('t1')
//            ->field('t1.id,t1.status,t1.gc_name,t1.pid,t2.gc_name as pname')
            ->join('expbao_operation t2','t1.oid = t2.id','LEFT')
            ->where('uid',$user_id)
            ->order('order','asc')
            ->select();
            var_dump(ExOptUserModel::getlastsql());
        $this->assign('data_list',$data_list);
        $this->assign('useropt', $useropts);
        $this->assign('user_id', $user_id);
        $this->assign('userinfo', $userinfo);
        //$this->assign('test', $test);
        }else{
            //$test="未登录";
            //echo "没有登录";
            //$xxx=ExUserModel::test();
            //echo $xxx;
           //$this->error("没有登录");
           $this->error('没有登录', url('ucenter/login'));
        }
            return $this->fetch(); 
        }
    }
        public function driver()
    {
       
       if ($this->request->isPost()) {

        } else {
            $user_id=$this->is_exp_login();
            if($user_id){
            //echo "已经登录";
           //$test="已经登录";
           //$userinfo = ExUserModel::where($user_id)->find();
           $request= \think\Request::instance();
           if($request){
                $fun=$request->action();
                $optid=ExOptModel::getidbyfun($fun);
                var_dump($optid);
                $isactive=ExOptUserModel::where('oid',$optid)->value('isactive');
                $opt_user_id=ExOptUserModel::where('oid',$optid)->value('id');
                var_dump($isactive);
                if($isactive==0){
                    $this->error('没有访问权限',url('/expbao/ucenter/active/optuserid/'.$opt_user_id));
                    var_dump($isactive);
                }
           };
           $opts = ExOptModel::column('id');
            dump($request->module());
            dump($request->action());
           $userinfo=ExUserModel::getuserinfo($user_id);
           $useropt=ExUserModel::getuseropt($user_id);
           //var_dump($useropts);
           $data_list = ExOptUserModel::table('__EXPBAO_OPERATION_USER__')
            ->alias('t1')
//            ->field('t1.id,t1.status,t1.gc_name,t1.pid,t2.gc_name as pname')
            ->join('expbao_operation t2','t1.oid = t2.id','LEFT')
            ->where('uid',$user_id)
            ->order('order','asc')
            ->select();

        $this->assign('data_list',$data_list);
        //$this->assign('useropt', $useropts);
        $this->assign('user_id', $user_id);
        $this->assign('userinfo', $userinfo);
        //$this->assign('test', $test);
        }else{
            //$test="未登录";
            //echo "没有登录";
            //$xxx=ExUserModel::test();
            //echo $xxx;
           //$this->error("没有登录");
           $this->error('没有登录', url('ucenter/login'));
        }
            return $this->fetch(); 
        }
    }

    public function active($optid='')
    {
        if ($optid === null ||$optid=='') $this->error('缺少模块参数');
        var_dump($optid);
        //echo '</br>optuserid:'.$optuserid;
        $user_id=$this->is_exp_login();
        //echo '</br>user_id:'.$user_id;
        //$optid=ExOptUserModel::getoptidbyid($optuserid);
        //echo '</br>optid:'.$optid;
        $opt=ExOptModel::getoptbyid($optid);
        //echo '</br>opt:'.$opt;
        $this->assign('user_id', $user_id);
        //$this->assign('optuserid', $optuserid);
        $this->assign('optid', $optid);
        $this->assign('opt', $opt);
        return $this->fetch(); 
    }

}